﻿using System;
using System.Net;
using Enyim.Caching;
using System.Web.Mvc;
using SilenthinkContract;

namespace Web.Manage.Controllers
{
    public abstract class BaseController : Controller
    {
        protected ILogService LogService { get; private set; }
        protected IBaseService BaseService { get; private set; }
        protected IExtraService ExtraService { get; private set; }
        protected IStoreService StoreService { get; private set; }
        protected IOrderService OrderService { get; private set; }
        protected IGameService GameService { get; private set; }
        protected IAuctionService AuctionService { get; private set; }
        protected IPassportService PassportService { get; private set; }
        protected IPaymentService PaymentService { get; private set; }
        protected static IMemcachedClient MemcachedClient { get; private set; }

        protected BaseController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
        {
            LogService = logService;
            BaseService = baseService;
            ExtraService = extraService;
            StoreService = storeService;
            OrderService = orderService;
            GameService = gameService;
            AuctionService = auctionService;
            PassportService = passportService;
            PaymentService = paymentService;
            MemcachedClient = memcachedClient;
        }
        
        protected JsonResult Json(HttpStatusCode statusCode, string message)
        {
            return Json(statusCode, message, false, string.Empty, string.Empty);
        }

        protected JsonResult Json(HttpStatusCode statusCode, string message, bool closeCurrent, string navTabId = null, string forwardUrl = null, string rel = null)
        {
            var callbackType = string.Empty;
            if (closeCurrent)
            {
                callbackType = "closeCurrent";
            }
            var obj = new { statusCode, message, navTabId, rel, callbackType, forwardUrl };
            return Json(obj);
        }
        protected JsonResult Json(HttpStatusCode statusCode, string message, string dialogId, string dialogUrl)
        {

            var obj = new { statusCode, message, dialogId, dialogUrl };
            return Json(obj);
        }


        private static readonly char[] Constant =
            {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
                'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
                'v', 'w', 'x', 'y', 'z',
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
                'V', 'W', 'X', 'Y', 'Z'
            };

        public enum RandomType
        {
            All,
            Number,
            Uppercased,
            Lowercased,
            NumberAndUppercased,
            NumberAndLowercased,
            UppercasedAndLowercased
        }

        public static string GenerateRandom(int length, RandomType randomType)
        {
            var initsize = 0;
            var beginsize = 0;
            var endsize = 0;
            var isCross = false;
            switch (randomType)
            {
                case RandomType.All:
                    {
                        initsize = 62;
                        beginsize = 1;
                        endsize = 62;
                        break;
                    }
                case RandomType.Lowercased:
                    {
                        initsize = 26;
                        beginsize = 11;
                        endsize = 36;
                        break;
                    }
                case RandomType.Uppercased:
                    {
                        initsize = 26;
                        beginsize = 37;
                        endsize = 62;
                        break;
                    }
                case RandomType.Number:
                    {
                        initsize = 10;
                        beginsize = 1;
                        endsize = 10;
                        break;
                    }
                case RandomType.UppercasedAndLowercased:
                    {
                        initsize = 52;
                        beginsize = 11;
                        endsize = 62;
                        break;
                    }
                case RandomType.NumberAndLowercased:
                    {
                        initsize = 36;
                        beginsize = 1;
                        endsize = 36;
                        break;
                    }
                case RandomType.NumberAndUppercased:
                    {
                        isCross = true;
                        break;
                    }
            }
            var newRandom = new System.Text.StringBuilder(initsize);
            var rd = new Random();
            if (!isCross)
            {
                for (var i = 0; i < length; i++)
                {
                    newRandom.Append(Constant[rd.Next(beginsize, endsize)]);
                }
            }
            else
            {
                for (var i = 0; i < length; i++)
                {
                    newRandom.Append(Constant[rd.Next(1, 10)]);
                    newRandom.Append(Constant[rd.Next(37, 62)]);
                }
            }
            return newRandom.ToString();
        }
    }
}